#__author__:"heng"
#__date__: $(DATE)

from atm.conf import setting
from os import walk, path, remove
import json
from tools.type_checking import check_type


def open_file(file_name: str) -> dict:
    try:
        user_file = open(path.join(setting.DB_FILE_PATH, file_name + ".json"))
        user_json = json.load(user_file)
        user_file.close()
        return user_json
    except Exception:
        raise


def write_file(file_name: str, **kw):
    try:
        user_file = open(path.join(setting.DB_FILE_PATH, file_name + ".json"), mode="w", encoding="utf-8")
        json.dump(kw, user_file)
        user_file.close()
    except Exception:
        raise


def create_account(**kw):
    if path.exists(path.join(setting.DB_FILE_PATH, kw["account"] + ".json")):
        raise FileExistsError("file is exist, you can open it")
    else:
        try:
            write_file(kw["account"], **kw)
        except Exception:
            raise


def edit_account(**kw):
    try:
        write_file(kw["account"], **kw)
    except Exception:
        raise


def read_account(account: str) -> dict:
    try:
        user_json = open_file(account)
        return user_json
    except Exception:
        raise


def del_account(account: str):
    try:
        remove(path.join(setting.DB_FILE_PATH, account + ".json"))
    except Exception:
        raise


if __name__ == "__main__":
    user_dict = {
        "account": "1111",
        "password": "1111",
        "balance": None
    }
    edit_account(**user_dict)
